package cc.rengu.igas.cops.core.service.trans.unionpay;


import cc.rengu.igas.cops.common.enums.RespCodeEnum;
import cc.rengu.igas.cops.core.service.base.PayService;
import cc.rengu.igas.cops.facade.request.CancelCupsQrCodeRequest;
import cc.rengu.igas.cops.facade.response.CancelCupsQrCodeResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/***
 * 撤销银联标准二维码
 */
public class CancelCupsQrCodeService extends PayService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        XmlTreeUtil tree = new XmlTreeUtil();
        CancelCupsQrCodeRequest cancelCupsQrCodeRequest = new CancelCupsQrCodeRequest();
        ConvertUtil.convertOutput(cancelCupsQrCodeRequest);
        return cancelCupsQrCodeRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        BizResponse bizResponse = new BizResponse();
        XmlTreeUtil tree = new XmlTreeUtil();
        CancelCupsQrCodeRequest cancelCupsQrCodeRequest = (CancelCupsQrCodeRequest) request;
        String qrCode = cancelCupsQrCodeRequest.getQrCode();
        String acqCode = XmlConfigUtil.getXmlConfigCacheKeyValue("upqc", "acqInsCode");
        if (qrCode.contains("/" + acqCode + "/")) {
            String[] qrCodeParams = qrCode.split(acqCode + "/");
            String qrCodeParam = qrCodeParams[qrCodeParams.length - 1];
            /* 收单发码 */
            bizResponse = dirCancelQrCodeService(cancelCupsQrCodeRequest, qrCodeParam);
        } else {
            /* 银联发码 */
            bizResponse = cupsCancelQrCodeService(cancelCupsQrCodeRequest);
        }
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        CancelCupsQrCodeResponse genCupsQrCodeResponse = (CancelCupsQrCodeResponse) bizResponse.getResult();
        ConvertUtil.convertInput(genCupsQrCodeResponse);
    }


    /**
     * 收单发码
     *
     * @param cancelCupsQrCodeRequest 扫码请求信息
     * @return 业务调用结果
     */
    BizResponse dirCancelQrCodeService(CancelCupsQrCodeRequest cancelCupsQrCodeRequest, String qrCodeParam) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysNo = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        BizResponse bizResponse = new BizResponse();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        /*采用“协议://域名 /机构代码/自定义部分”的格式,自定义部分为二维码类型&订单号*/
        String[] strArr = qrCodeParam.split("&");
        if (strArr.length != 2) {
            rglog.error("不支持的二维码参数qrCodeParam=[{}]", qrCodeParam);
            throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
        }
        String qrCodeType = strArr[0];
        if ("0".equals(qrCodeType)) {
            //动态码,获取订单信
           /* OrderInfoMapper payOrderMapper = new OrderInfoMapperImpl();
            OrderInfo payOrderInfo = payOrderMapper.selectIgasOrderInfoByOrderId(instId,strArr[1]);
            if (null == payOrderInfo){
                rglog.error("获取支付订单信息失败,payOrderId:<{}>,TraceNo:<{}>,mchntNo:<{}>",strArr[1],cancelCupsQrCodeRequest.getTraceNo(),cancelCupsQrCodeRequest.getMchntNo());
                throw new BizException(RespCodeEnum.GET_PAY_ORDER_INFO_ERROR.getRespCode(), RespCodeEnum.GET_PAY_ORDER_INFO_ERROR.getRespDesc());
            }
            *//* 检查订单状态 *//*
            if (OrderStatusEnum.PAY_SUCCESS.getStatus().equals(payOrderInfo.getOrderStatus())){
                rglog.error("当前二维码已支付完成，不允许撤销!",payOrderInfo.getOrderStatus());
                throw new BizException(RespCodeEnum.ORDER_CANT_CACEL.getRespCode(), RespCodeEnum.ORDER_CANT_CACEL.getRespDesc());
            }
            updateOrderStatus(payOrderInfo.getInstId(),payOrderInfo.getOrderId(),OrderStatusEnum.CLOSED.getStatus(),PayStatusEnum.SUCCESS.getStatus(),payOrderInfo.getPayId());*/

        } else if ("1".equals(qrCodeType)) {
            //静态码

        } else {
            rglog.error("不支持的二维码类型qrCodeType=[{}]", qrCodeType);
            throw new BizException(RespCodeEnum.NOT_SURP_TRANS_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SURP_TRANS_TYPE_ERROR.getRespDesc());

        }

        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }


    /**
     * 撤销银联二维码
     *
     * @param cancelCupsQrCodeRequest
     * @return 业务调用结果
     */
    BizResponse cupsCancelQrCodeService(CancelCupsQrCodeRequest cancelCupsQrCodeRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String sysNo = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);

       /* UpqcService upacService = new UpqcServiceImpl();
        UpqcQrCodeCancelRequest upqcQrCodeCancelRequest =  new UpqcQrCodeCancelRequest();
        upqcQrCodeCancelRequest.setTransSeqNum(sysNo);
        upqcQrCodeCancelRequest.setTransDateTime(sysNo.substring(8,22));
        upqcQrCodeCancelRequest.setQrCode(cancelCupsQrCodeRequest.getQrCode());
        UpqcQrCodeCancelResponse upqcQrCodeCancelResponse = upacService.upqcQrCodeCancel(upqcQrCodeCancelRequest);
        if(!CopsParamConstant.UPAC_STRING_SUCCESS.equals(upqcQrCodeCancelResponse.getRespCode())){
            rglog.error("调用银联全渠道服务失败,respCode=[{}],respDesc{}",upqcQrCodeCancelResponse.getRespCode(),upqcQrCodeCancelResponse.getRespDesc());
            throw new BizException(upqcQrCodeCancelResponse.getRespCode(),upqcQrCodeCancelResponse.getRespDesc());
        }*/

        /* 更新支付订单状态为支付中 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }


    /**
     * 更新支付订单状态
     * @param instId 机构标识
     * @param orderId 订单号
     * @param orderStatus 订单状态
     * @param payStatus 支付状态
     * @param payId 支付流水号
     */
/*
    private void updateOrderStatus(String instId,String orderId,String orderStatus,String payStatus,String payId) throws Exception {
        OrderInfoMapper orderInfoMapper = new OrderInfoMapperImpl();
        int iReturnCode = orderInfoMapper.updateIgasOrderStatusAndPayId(instId,orderId,orderStatus,payStatus,payId);
        if (0 != iReturnCode) {
            rglog.error("更新支付订单状态失败,orderId:<{}>,OrderStatus:<{}>,payStatus:<{}>", orderId,orderStatus,payStatus);
            throw new BizException(RespCodeEnum.UPDATE_ORDER_STATUS_ERROR.getRespCode(), RespCodeEnum.UPDATE_ORDER_STATUS_ERROR.getRespDesc());
        }
    }
*/
}
